package com.otod.pm.prj.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.otod.base.exception.AppException;
import com.otod.base.json.JsonSuccess;
import com.otod.commons.auth.SessionUserHolder;
import com.otod.commons.auth.SessionUserUtil;
import com.otod.commons.constants.CommonConstant;
import com.otod.pm.prj.service.PmStepDocCfgService;
import com.otod.pm.prj.service.ProjectService;
import com.otod.pm.prj.vo.PmStepDocCfgVo;
import com.otod.pm.prj.vo.ProjectVo;

/**
 * 
 * @author long
 *
 */
@Controller
@RequestMapping("pm/pmStepDocCfg")
public class PmStepDocCfgController {
	
	@Autowired
	PmStepDocCfgService pmStepDocCfgService;
	@Autowired
	ProjectService projectService;
	

	
	/**
	 * 通过uuid查询项目信息 如果pmStepDocCfg没有数据则添加到pmStepDocCfg表中
	 * @param stepId 就是 uuid
	 * @return
	 */
	@RequestMapping(value="selectProject",method = RequestMethod.GET)
	@ResponseBody
	public PmStepDocCfgVo selectProject(String uuid){
		PmStepDocCfgVo pmStepDocCfgVo = null;
		pmStepDocCfgVo = pmStepDocCfgService.selectStepFoler(uuid);
		//如果没有就创建文件夹
		if(pmStepDocCfgVo==null){
			SessionUserHolder sessionUser = SessionUserUtil.getCurrentUser();
			//通过uuid项目的相关的信息
			ProjectVo projectVo = projectService.getById(uuid);
			pmStepDocCfgVo = pmStepDocCfgService.addFolderOrFile(projectVo.getUuid(), "", CommonConstant.FOLDER, projectVo.getPrjName(), "", sessionUser.getUserNo());
		}
		return pmStepDocCfgVo; 
	}
	
	
	/**
	 * 通过uuid查询阶段文件夹 没有则创建
	 * @param stepId 就是 uuid
	 * @return
	 */
	@RequestMapping(value="selectStepFoder",method = RequestMethod.GET)
	@ResponseBody
	public PmStepDocCfgVo selectStepFoder(String uuid,String folderName,String parentId){
		PmStepDocCfgVo pmStepDocCfgVo = null;
		pmStepDocCfgVo = pmStepDocCfgService.selectStepFoler(uuid);

		//如果没有就创建文件夹
		if(pmStepDocCfgVo==null){
			SessionUserHolder sessionUser = SessionUserUtil.getCurrentUser();
			pmStepDocCfgVo = pmStepDocCfgService.addFolderOrFile(uuid, "", CommonConstant.FOLDER, folderName, parentId, sessionUser.getUserNo());		
															
		}
		return pmStepDocCfgVo; 
	}
	

	
	
	/**
	 * 添加修改 文件 或者文件夹
	 * @param uuid
	 * @param stepId
	 * @param folderType
	 * @param folderName
	 * @param parentId
	 * @return
	 * @throws AppException 
	 */
	@RequestMapping(value="addFolderOrFile",method = RequestMethod.GET)
	@ResponseBody
	public PmStepDocCfgVo addFolderOrFile(String uuid,String stepId,String folderType,String folderName,String parentId ){
		PmStepDocCfgVo pmStepDocCfgVo = null;
		pmStepDocCfgVo = pmStepDocCfgService.selectStepFoler(uuid);
		
		//如果不存在就插入数据
		if(pmStepDocCfgVo==null){
			SessionUserHolder sessionUser = SessionUserUtil.getCurrentUser();
			pmStepDocCfgVo=pmStepDocCfgService.addFolderOrFile( uuid, stepId, folderType, folderName, parentId,sessionUser.getUserNo());
		//存在就修改数据	
		}else{
			pmStepDocCfgService.updateaddFolderOrFile(uuid, stepId, folderType, folderName, parentId);
			
		}
		return pmStepDocCfgVo;
	}
	
	
	/**
	 * 查询 阶段成果下面的所有数据
	 * @return
	 */
	@RequestMapping(value="queryForList",method = RequestMethod.GET)
	@ResponseBody
	public List<PmStepDocCfgVo> queryForList(String stepId){
		List<PmStepDocCfgVo> list = Lists.newArrayList();
		list = pmStepDocCfgService.queryForList(stepId);
		return list;
	} 
	
	/**
	 * 通过uuid 删除 数据
	 * @param uuid
	 * @return
	 */
	@RequestMapping(value="deleteFolderOrFile",method = RequestMethod.GET)
	@ResponseBody
	public String deleteFolderOrFile(String uuid){
		
		pmStepDocCfgService.deleteById(uuid);
		return JsonSuccess.factory("删除成功");
	}
	
	
	/**
	 * 查询名字是否重复 主要是正对ztree菜单
	 * @param folderName
	 * @param parentId
	 * @return
	 * @throws AppException
	 */
	@RequestMapping(value="selectRepetitionFolderName",method = RequestMethod.GET)
	@ResponseBody											
	public PmStepDocCfgVo selectRepetitionFolderName(String folderName,String parentId,String stepId){
		PmStepDocCfgVo pmStepDocCfgVo = null;
		pmStepDocCfgVo = pmStepDocCfgService.selectRepetitionFolderName(folderName,parentId,stepId);
		if(pmStepDocCfgVo==null){
			return null;
		}
		return pmStepDocCfgVo;
	}
	
	
	/**
	 * 修改项目名称 和 阶段名称
	 * @param uuid
	 * @param folderName
	 * @return
	 * @throws AppException 
	 */
	@RequestMapping(value="updateProjectName",method = RequestMethod.GET)
	@ResponseBody	
	public String updateProjectName(String uuid,String folderName,String stepId) throws AppException{
		pmStepDocCfgService.update(uuid, folderName, stepId);
		return JsonSuccess.factory("修改成功");
	}
	
	
	/**
	 * 添加项目文库
	 * @param prjId
	 * @return
	 * @throws AppException 
	 */
	@RequestMapping(value="addProjectDoc",method = RequestMethod.GET)
	@ResponseBody	
	public String addProjectDoc(String prjId,String stepId) throws AppException{
	
		pmStepDocCfgService.addProjectDoc(prjId,stepId);
		return JsonSuccess.factory("添加项目文库成功");
	}
	
	
}
